package cn.worldyao.homework.service.impl;

import cn.worldyao.homework.Tools;
import cn.worldyao.homework.entity.Teachers;
import cn.worldyao.homework.mapper.TeachersMapper;
import cn.worldyao.homework.service.TeachersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yaozi
 * @since 2020-05-07
 */
@Service
@Slf4j
public class TeachersServiceImpl extends ServiceImpl<TeachersMapper, Teachers> implements TeachersService {

    @Override
    public boolean checkTLoginNameExist(String loginName) {
        log.info("\n-----checkLoginNameExist-[{}]----\n", loginName);
        QueryWrapper<Teachers> queryWrapper;
        queryWrapper = new QueryWrapper();
        queryWrapper.eq("LOGIN_NAME", loginName);
        int i = this.count(queryWrapper);

        return i > 0;
    }

    @Override
    public Teachers tLogin(Teachers teachers) {
        Teachers teachers1 = this.getTOneByLoginName(teachers.getLoginName());
        if (teachers1 != null) {
            String ePass = Tools.MD5Pwd(teachers.getPassword(), teachers1.getSalt());
            if (ePass.equals(teachers1.getPassword())) {
                return teachers1;
            } else {
                return null;
            }
        }
        return null;
    }

    @Override
    public Teachers getTOneByLoginName(String loginName) {
        QueryWrapper<Teachers> wrapper = new QueryWrapper<>();
        wrapper.eq("LOGIN_NAME", loginName);
        return this.getOne(wrapper);
    }
}
